<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
	<HEAD>
		<META CONTENT="text/html; charset=UTF-8" HTTP-EQUIV="Content-Type">
		<TITLE>PI.JS > pi.comet</TITLE>
		<LINK HREF="../../../inc/template.css" REL="stylesheet">
		<LINK HREF="../../../inc/template.css" REL="stylesheet">
		<SCRIPT SRC="../../../source/pi.1.1.1.js" TYPE="text/javascript"></SCRIPT>
		<SCRIPT SRC="../../../inc/template.js" TYPE="text/javascript"></SCRIPT>
		<SCRIPT SRC="../../../plugin/pi.comet/pi.comet.1.2.js" TYPE="text/javascript"></SCRIPT>
		<LINK HREF="../../../inc/sh/SyntaxHighlighter.css" TYPE="text/css" REL="stylesheet">
		<SCRIPT SRC="../../../inc/sh/shCore.js" LANGUAGE="javascript"></SCRIPT>
		<SCRIPT SRC="../../../inc/sh/shBrushJScript.js" LANGUAGE="javascript"></SCRIPT>
		<SCRIPT SRC="../../../inc/sh/shBrushCss.js" LANGUAGE="javascript"></SCRIPT>
		<SCRIPT SRC="../../../inc/sh/shBrushXml.js" LANGUAGE="javascript"></SCRIPT>
		<SCRIPT SRC="../../../inc/sh/shBrushPhp.js" LANGUAGE="javascript"></SCRIPT>
		<SCRIPT SRC="../../../inc/sh/shBrushPython.js" LANGUAGE="javascript"></SCRIPT>
	</HEAD>
	<BODY>
		<A HREF="/" ID="header" CLASS="Header">
			<EM></EM>
		</A>
		<DIV CLASS="Index">
			<DIV ID="indexContainer" CLASS="ListContainer">
				<A HREF="../../../index.html" CLASS="Button "><EM>About Pi</EM></A>
				<A HREF="../../../pages/Downloads" CLASS="Button "><EM>Downloads</EM></A>
				<A HREF="../../../documentation/index.html" CLASS="Button SelectedButton"><EM>Documentation</EM></A>
				<DIV CLASS="SubNavigation">
					<DIV>
						<H2 CLASS="Title">Essential Documents</H2>
						<UL>
							<LI>
								<A STYLE="" HREF="../../../documentation/essential/Getting Started to pi.js/">Getting Started to pi.js</A>
							</LI><LI>
								<A STYLE="" HREF="../../../documentation/essential/Object Oriented Programming with pi.js/">Object Oriented Programming with pi.js</A>
							</LI>
						</UL>
					</DIV><DIV>
						<H2 CLASS="Title">Reference</H2>
						<UL>
							<LI>
								<A STYLE="" HREF="../../../documentation/env/pi.env/">pi.env</A>
							</LI><LI>
								<A STYLE="" HREF="../../../documentation/get/pi.get/">pi.get</A>
							</LI><LI>
								<A STYLE="font-weight:bold" HREF="../../../documentation/util/pi.util/">pi.util</A>
								<UL>
									<LI>
										<A HREF="../../../documentation/util/pi.util.Array/">pi.util.Array</A>
									</LI><LI>
										<A HREF="../../../documentation/util/pi.util.Element/">pi.util.Element</A>
									</LI><LI>
										<A HREF="../../../documentation/util/pi.util.Hash/">pi.util.Hash</A>
									</LI><LI>
										<A HREF="../../../documentation/util/pi.util.Number/">pi.util.Number</A>
									</LI><LI>
										<A HREF="../../../documentation/util/pi.util.String/">pi.util.String</A>
									</LI>
								</UL>
							</LI><LI>
								<A STYLE="" HREF="../../../documentation/base/pi.base/">pi.base</A>
							</LI><LI>
								<A STYLE="" HREF="../../../documentation/element/pi.element/">pi.element</A>
							</LI><LI>
								<A STYLE="" HREF="../../../documentation/xhr/pi.xhr/">pi.xhr</A>
							</LI><LI>
								<SPAN STYLE="font-weight:bold">Native Objects</SPAN>
								<UL>
									<LI>
										<A HREF="../../../documentation/Native Objects/Array/">Array</A>
									</LI><LI>
										<A HREF="../../../documentation/Native Objects/Function/">Function</A>
									</LI><LI>
										<A HREF="../../../documentation/Native Objects/Number/">Number</A>
									</LI><LI>
										<A HREF="../../../documentation/Native Objects/String/">String</A>
									</LI>
								</UL>
							</LI><LI>
								<SPAN STYLE="font-weight:bold">Plugins</SPAN>
								<UL>
									<LI>
										<A HREF="../../../documentation/plugin/pi.comet/">pi.comet</A>
									</LI><LI>
										<SPAN>pi.cache</SPAN>
									</LI><LI>
										<SPAN>pi.layer</SPAN>
									</LI><LI>
										<A HREF="../../../documentation/plugin/pi.storage/">pi.storage</A>
									</LI><LI>
										<SPAN>pi.win</SPAN>
									</LI>
								</UL>
							</LI>
						</UL>
					</DIV>
				</DIV>
				<A HREF="../../../pages/Plugins" CLASS="Button "><EM>Plugins</EM></A>
				<A HREF="http://groups.google.com/group/pi-js" CLASS="Button"><EM>Developer Forum</EM></A>
				<A HREF="http://code.google.com/p/pi-js/issues/list" CLASS="Button"><EM>Issues</EM></A>
			</DIV>
		</DIV>
		<!-- <h3 class="ContentNav" id="contentTitle">
			<div>
				<a class="Sel">index.html</a>
			</div>
		</h3>-->
		<DIV ID="documentationContent" CLASS="Content">
			<DIV CLASS="Pad">
				<H1 CLASS="Name">pi.comet<EM CLASS="DocumentationTitleVersion">2009-01-06 09:23:21</EM></H1>
				<DIV>
				<H2 CLASS="Title">Summary</H2>
				pi.comet is a simple javascript class to create cross browser Comet(Reverse Ajax) applications easily.It provides realtime data transfers between client and server.You can use pi.comet with any serverside language, but I recommend Python(<A HREF="http://cherrypy.org">CherryPy</A>) and <A HREF="http://php.net">PHP</A>.
				</DIV>
				<DIV CLASS="Text">
					<STRONG>Download:</STRONG> <A HREF="http://pi-js.googlecode.com/files/pi.comet.1.2.tar.gz">4.1KB</A>
<H2 CLASS="Title">Demo</H2>
<UL>
	<LI><H4 CLASS="SubTitle">Realtime Web Chat (PHP)</H4>- <A HREF="examples/chat/php/form.php">Try</A><BR> - <A HREF="http://pi-js.googlecode.com/files/chat_example.tar.gz">Source</A></LI>
	<LI><H4 CLASS="SubTitle">Realtime Web Chat (CherryPy<EM>(Python)</EM>)</H4>- <A HREF="http://pi-js.googlecode.com/files/chat_example.tar.gz">Source</A></LI>
</UL>
<H2 CLASS="Title">Syntax</H2>
<CODE>var stream = new pi.comet(<EM>name (optional)</EM>,<EM>onread (optional)</EM>,<EM>onclose (optional)</EM>);</CODE>
<H2 CLASS="Title">Usage Example</H2>
Quick usage:
<CODE>var stream = pi.comet.get(<EM>url</EM>,<EM>onread</EM>,<EM>onclose</EM>);</CODE>
General Usage:
<PRE NAME="code" CLASS="js">
var stream = new pi.comet;
stream.environment.setName('test');
stream.environment.setUrl('push.php');
stream.event.push = function(_response){ alert(_response); };
stream.event.disconnect = function(){ alert('Connection is closed'); };
stream.environment.send();
</PRE>
There is a one more alternative usage too, you can use the <A HREF="StaticMethods/get/">"get" static method</A>:
<PRE NAME="code" CLASS="js">
var stream = pi.comet.get(
	'push.php',
	function(_response){ alert(response); },
	function(){ alert('Connection is closed'); }
);
</PRE>
				</DIV>
				<DIV CLASS="PageListContainer">
					<DIV CLASS="Page">
						<H2 CLASS="Title">Methods</H2>
						<DIV CLASS="PageItem">
							<A HREF="Methods/abort/" CLASS="PageItemTitle">abort</A>
							<DIV CLASS="PageItemSummary">Cancels the request.</DIV>
						</DIV><DIV CLASS="PageItem">
							<A HREF="Methods/send/" CLASS="PageItemTitle">send</A>
							<DIV CLASS="PageItemSummary">Sends the request.</DIV>
						</DIV><DIV CLASS="PageItem">
							<A HREF="Methods/environment.setUrl/" CLASS="PageItemTitle">environment.setUrl</A>
							<DIV CLASS="PageItemSummary">Sets transfer path.</DIV>
						</DIV>
					</DIV><DIV CLASS="Page">
						<H2 CLASS="Title">Events</H2>
						<DIV CLASS="PageItem">
							<A HREF="Events/push/" CLASS="PageItemTitle">push</A>
							<DIV CLASS="PageItemSummary">When the target url prints data between "&lt;comet>" and "&lt;/comet>" tags, the data is sent to event.push.</DIV>
						</DIV><DIV CLASS="PageItem">
							<A HREF="Events/disconnect/" CLASS="PageItemTitle">disconnect</A>
							<DIV CLASS="PageItemSummary">Runs when the stream is closed.</DIV>
						</DIV>
					</DIV><DIV CLASS="Page">
						<H2 CLASS="Title">StaticMethods</H2>
						<DIV CLASS="PageItem">
							<A HREF="StaticMethods/get/" CLASS="PageItemTitle">get</A>
							<DIV CLASS="PageItemSummary">Opens a comet channel and returns the pi.comet object.</DIV>
						</DIV>
					</DIV><DIV CLASS="Page">
						<H2 CLASS="Title">Serverside Scripting</H2>
						<DIV CLASS="PageItem">
							<A HREF="Serverside Scripting/How it works/" CLASS="PageItemTitle">How it works</A>
							<DIV CLASS="PageItemSummary">Understanding the push technology.</DIV>
						</DIV><DIV CLASS="PageItem">
							<A HREF="Serverside Scripting/Push Classes/" CLASS="PageItemTitle">Push Classes</A>
							<DIV CLASS="PageItemSummary">The classes which provide cross browser outputs.</DIV>
						</DIV>
					</DIV>
				</DIV>
				<DIV CLASS="Block">
					<H1>Examples</H1>
					<DIV CLASS="Example">
						<H1>Example #1: simple data stream</H1>
							<DIV CLASS="View">
								<BUTTON ID="start">Start</BUTTON><BUTTON ID="stop">Stop</BUTTON>
<TEXTAREA ID="monitor">
</TEXTAREA><LINK HREF="examples/simple/simple.css" TYPE="text/css" REL="stylesheet"><SCRIPT SRC="examples/simple/simple.js" TYPE="text/javascript"></SCRIPT>
							</DIV>
							<H2>Source files of the above example:</H2>
							<DIV>
								<H3>examples/simple/simple.html</H3>
								<PRE NAME="code" CLASS="html">&lt;button id="start">Start&lt;/button>&lt;button id="stop">Stop&lt;/button>
&lt;textarea id="monitor">
&lt;/textarea></PRE>
							</DIV><DIV>
								<H3>examples/simple/simple.css</H3>
								<PRE NAME="code" CLASS="css">#monitor { display:block; width:400px; height:150px; }</PRE>
							</DIV><DIV>
								<H3>examples/simple/simple.js</H3>
								<PRE NAME="code" CLASS="js">var stream;
function start(){
	stream = pi.comet.get("examples/simple/simple.php",read,disable);
	startButton.attribute.set("disabled","disabled").event.removeListener("click",start);
	stopButton.attribute.remove("disabled").event.addListener("click",stop);
	textbox.update("");
}

function read(_response){
	textbox.element.value+="\n"+_response;
}

function stop(){
	stream.abort();
	disable();
}

function disable(){
	startButton.attribute.remove("disabled").event.addListener("click",start);
	stopButton.attribute.set("disabled","disabled").event.removeListener("click",stop);
}

var startButton = new pi.element(pi.get("start")).event.addListener("click",start), 
stopButton = new pi.element(pi.get("stop")).attribute.set("disabled","disabled"), 
textbox = new pi.element(pi.get("monitor")).attribute.set("readonly","readonly");</PRE>
							</DIV>
							<DIV>
								<DIV>
									<H3>examples/simple/simple.php</H3>
									<PRE NAME="code" CLASS="php">&lt;?
	require_once "pi_comet.php";
	
	$stream = new PIComet($_GET["PICometMethod"],$_GET["PICometName"]);
	
	header("Content-type:".$stream->header);
	
	$messages = Array("If the number's there I'll find it!","Something's going on.","It has to do with that number.","There's an answer in that number.","You see the simplicity of the circle.","Mathematics is the language of nature","Everything around us can be represented and understood through numbers","If you graph these numbers, patterns emerge.","Therefore: There are patterns everywhere in nature.");
	
	for($i=0; $i&lt;15; $i++){
		print $stream->push( time() . " " . $messages[rand(0,count($messages)-1)]  );
		ob_flush();
		flush();
		sleep(1);
	}
?></PRE>
								</DIV>
							</DIV>
					</DIV>
				</DIV>
			</DIV>
		</DIV>
		<DIV CLASS="Clean"></DIV>
	</BODY> 
</HTML>